package com.ihighest.app.infox.tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.ihighest.app.infox.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.EmailHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 分享初始化工具
 * 
 * @author lkx Created at 2015-10-16 下午4:44:46
 * @version 1.0
 */
public class InitShare {
	private Context context;
	public static final String DESCRIPTOR = "com.umeng.share";
	private static UMSocialService mController = null;
	private final String QQAPPkey = "cEfRpStXiNvDZXrs";
	private final String QQAPPID = "1104945202";
	// private final String QQAPPID = "1104807457";

	// private final String QQAPPkey = "Z4bkvUVJNtwmDvtK";
	private UMWXHandler wxHandler;
	private UMWXHandler wxCircleHandler;
	private WeiXinShareContent weixinContent;
	private CircleShareContent circleMedia;
	private SmsHandler smsHandler;
	private EmailHandler emailHandler;
	private UMQQSsoHandler qqHandler;
	private SinaShareContent sinaShareContent;
	private QQShareContent qqShareContent;

	public InitShare(Context context) {
		this.context = context;
		initUMService();
	}

	/**
	 * @Title: initUMService
	 * @Description: 初始化
	 * @throws
	 */
	private void initUMService() {
		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
			// 关闭提示
			mController.getConfig().closeToast();
		}
		configPlatforms();
	}

	/**
	 * 初始化一些平台
	 * 
	 * @return
	 */
	private void configPlatforms() {
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = "wx23c518cef9735be5";
		String appSecret = "d4624c36b6795d1d99dcf0547af5443d";
		wxHandler = new UMWXHandler(context, appId, appSecret);
		wxHandler.addToSocialSDK();

		wxCircleHandler = new UMWXHandler(context, appId, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		weixinContent = new WeiXinShareContent();
		circleMedia = new CircleShareContent();
		smsHandler = new SmsHandler();
		smsHandler.addToSocialSDK();
		emailHandler = new EmailHandler();
		emailHandler.addToSocialSDK();

		qqHandler = new UMQQSsoHandler((Activity) context, QQAPPID, QQAPPkey);
		qqHandler.addToSocialSDK();
		qqShareContent = new QQShareContent();

		// se tShareContent(qqShareContent);

		sinaShareContent = new SinaShareContent();

		// 设置文字分享内容
		// setMyShareContent((context.getString(R.string.app_name)) , "");
		// 图片分享内容
		// mController.setShareMedia(new UMImage(context,
		// R.drawable.ic_launcher));

	}

	/**
	 * @功能描述 : 设置对应平台分享的内容 baseShareContent 需要传入分享的平台
	 * @return
	 */
	public void setMyShareContent(String content, String url) { // 单独设置分享内容
		// 设置分享内容
		mController.setShareContent(content + url);
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
		wxHandler.setTargetUrl(url);
		wxCircleHandler.setTargetUrl(url);
		weixinContent.setShareImage(new UMImage(context, bitmap));
		weixinContent.setTargetUrl(url);
		circleMedia.setTargetUrl(url);
		smsHandler.setTargetUrl(url);
		emailHandler.setTargetUrl(url);
		qqHandler.setTargetUrl(url);
		sinaShareContent.setTargetUrl(url);
		qqShareContent.setShareImage(new UMImage(context, bitmap));
		qqShareContent.setTargetUrl(url);

	}

	/**
	 * 
	 * @Title: isShareSuccess
	 * @Description: 自定义友盟分享操作的接口回调方法
	 * @param @param platform
	 * @return void
	 * @throws
	 */

	// 判断分享是否成功
	public void isShareSuccess(SHARE_MEDIA share_media) {
		mController.postShare(context, share_media, new SocializeListeners.SnsPostListener() {
			@Override
			public void onStart() {
				// Toast.makeText(context, "开始分享.", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
				if (eCode == 200) {
					Toast.makeText(context, "分享成功.", Toast.LENGTH_SHORT).show();
				} else {
					String eMsg = "";
					if (eCode == -101) {
						eMsg = "没有授权";
					}
					// Toast.makeText(context, "分享失败[" + eCode + "] " + eMsg,
					// Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

}
